package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.Product;
import com.li.shop.domain.ProductExample;
import com.li.shop.domain.ProductPic;
import com.li.shop.mapper.ProductMapper;
import com.li.shop.service.DictionaryService;
import com.li.shop.service.ProductService;
import com.li.shop.utils.SearchProductResponse;
import com.li.shop.utils.SearchProductdictionaryidResponse;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    DictionaryService dictionaryService;

    @Override
    public List<Product> findByProductName(String productName) {
        return productMapper.listByProductName(productName);
    }
    @Override
    public List<Product> listAll() {
        return productMapper.listAll();
    }
    @Override
    public Product selectByPrimaryKey(String id) {
        return productMapper.selectByPrimaryKey(id);
    }
    @Override
    public int update(Product product) {
        return productMapper.update(product);
    }
    @Override
    public List<Product> selectByExample(ProductExample example) {
        return productMapper.selectByExample(example);
    }

    //根据商品价格排序
    @Override
    public List<Product> sortByPrice(String searchContent) {
        ProductExample example =new ProductExample();
        example.createCriteria().andProductNameLike("%"+searchContent+"%");       //得到搜索关键字的结果
        example.setOrderByClause("money ASC");
        List<Product> list=productMapper.selectByExample(example);
        return list;
    }

    //根据商品销量排序
    @Override
    public List<Product> sortBySalesVolume(String searchContent) {
        ProductExample example =new ProductExample();
        example.createCriteria().andProductNameLike("%"+searchContent+"%");       //得到搜索关键字的结果
        example.setOrderByClause("sales_volume ASC");
        List<Product> list=productMapper.selectByExample(example);
        return list;
    }

    @Override
    public void updateSalesVolume(String productId,Integer salesVolume) {
        Product product=productMapper.selectByPrimaryKey(productId);
        System.out.println("sale"+product.getSalesVolume());
        if (product.getSalesVolume()==null){        //判断一下本产品原来的销量是否为null，是的话就将销量置为0.不是null就直接加
            product.setSalesVolume(0);
        }else {
            product.setSalesVolume(product.getSalesVolume()+salesVolume);
        }
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public List<Product> listAll(String productName) {
        ProductExample example=new ProductExample();
        example.createCriteria();
        List<Product>products=productMapper.selectByExample(example);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> list(String dId) {
        ProductExample example=new ProductExample();
        example.createCriteria().andDictionaryidEqualTo(dId);
        List<Product>products=productMapper.selectByExample(example);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> list() {
        ProductExample example=new ProductExample();
        //example.setOrderByClause("pId desc");
        List<Product>products=productMapper.selectByExample(example);
        return products;
    }

    @Override
    public List<Product> getAll() {
        ProductExample example=new ProductExample();
        List<Product>products=productMapper.selectByExample(example);
        return products;
    }

    @Override
    public int saveProduct(Product product) {
        if(StringUtils.isNotBlank(product.getpId()))
        {
            return productMapper.updateByPrimaryKeySelective(product);
        }
        else {
            return productMapper.insert(product);
        }
    }

    @Override
    public Product getpId(String pId) {
        return productMapper.selectByPrimaryKey(pId);
    }

    @Override
    public void add(Product product) {
        productMapper.insert(product);
    }

    @Override
    public SearchProductResponse listUser(Integer pageSize, Integer pageNum, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        ProductExample example =new ProductExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andProductNameLike("%"+keyword+"%");
        }
        List<Product> products= productMapper.selectByExample(example);
        return  new SearchProductResponse(((Page)products).getTotal(),products);
    }


    @Override
    public int delete(String pId) {
        return productMapper.deleteByPrimaryKey(pId);
    }

    @Override
    public SearchProductdictionaryidResponse listUser2(Integer pageSize, Integer pageNum, String dictionaryid) {
        PageHelper.startPage(pageNum,pageSize);
        ProductExample example =new ProductExample();
        if (StringUtils.isNotBlank(dictionaryid)) {
            example.createCriteria().andDictionaryidEqualTo("%"+dictionaryid+"%");
        }
        List<Product> products= productMapper.selectByExample(example);
        return  new SearchProductdictionaryidResponse(((Page)products).getTotal(),products);

    }

    @Override
    public int updatename(Product product) {

        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public List<Product> selectAndcollect() {
        return productMapper.selectcollect();
    }

    @Override
    public List<Product> rexiao(String phname) {
        ProductExample example=new ProductExample();
        example.createCriteria().andProductNameEqualTo("%"+phname+"%");
        List<Product>product= productMapper.selectByExample(example);
        return product;
    }

    @Override
    public List<Product> findalllike(String pname) {
        return productMapper.findAllLikeProduct(pname);
    }
}
